package com.team.cluedo.app;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.team.cluedo.Game;
import com.team.cluedo.R;
import com.team.cluedo.app.utils.CluedoMenu;
import com.team.cluedo.app.utils.ImageAdaptater;
import com.team.cluedo.app.waitscreens.WaitForAskedCard;

public class MakeHypothese extends Activity {
	boolean persoSelected = false;
	boolean armeSelected = false;
	
	int characterPosition = 0;
	int weaponPosition = 0;
	
	private static final int CARD_WIDTH = 85;
	private static final int CARD_HEIGHT = 100;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		return CluedoMenu.onCreateOptionsMenu(menu, this);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return CluedoMenu.onOptionsItemSelected(item, this);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	    setContentView(R.layout.twogalleries);
	   
	    TextView info = (TextView) findViewById(R.id.TextViewInformation);
	    info.setText(R.string.makehypothesis);
	    
	    Gallery gPerso = (Gallery) findViewById(R.id.gallerycharacter);
	    gPerso.setAdapter(new ImageAdaptater(this,Game.cluedo.nameOfCharacter, CARD_WIDTH, CARD_HEIGHT));
	    gPerso.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView parent, View v, int position, long id) {
	            persoSelected = true;
	            characterPosition = position;
	            String toast = Game.cluedo.getNameOfCharacter(position);
	            if(armeSelected) {
	            	toast = toast + "\n" +Game.cluedo.getNameOfWeapon(weaponPosition);
	            }
	            Toast.makeText(MakeHypothese.this,toast , Toast.LENGTH_SHORT).show();
	        }
	    });
	    
	    Gallery gWeapons = (Gallery) findViewById(R.id.galleryweapon);
	    gWeapons.setAdapter(new ImageAdaptater(this,Game.cluedo.nameOfWeapon, CARD_WIDTH, CARD_HEIGHT));
	    gWeapons.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView parent, View v, int position, long id) {
	            armeSelected = true;
	            weaponPosition = position;
	            String toast = Game.cluedo.getNameOfWeapon(position);
	            if(persoSelected) {
	            	toast = Game.cluedo.getNameOfCharacter(characterPosition)+ "\n" + toast ;
	            }
	            Toast.makeText(MakeHypothese.this, toast, Toast.LENGTH_SHORT).show();
	        }
	    });
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		Intent defineIntent;
		if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
			if(persoSelected == true && armeSelected == true){
				Game.cluedo.waitForSuggestion(characterPosition, weaponPosition);
				defineIntent = new Intent(this,WaitForAskedCard.class);
				Game.cluedo.activity.startActivity(defineIntent);
				this.finish();
			}
			else {
				defineIntent = new Intent(this,MakeHypothese.class);
				this.startActivity(defineIntent);
			}
		}
	return false;
	}
}
